var e = getApp();
const {
  getPointList,
  getGoldList,
  createOrder,
  getBlance
} = require('../../common/api/user')
const api = require('../../common/utils/request.js')
Component({
  properties: {
    show: {
      value: false,
      type: Boolean
    },
    openid: {
      value: '',
      type: String
    }
  },

  data: {
    list: [],
    current: 0,
    price: 0,
    taocan_id: 0,
    taocan_type: 0,
    payTypeList: [{
      title: '余额支付',
      icon: '../../common/static/pay-jinbi-icon.png',
      type: 3,
    },
    {
      title: '微信支付',
      icon: '../../common/static/pay-wechat-icon.png',
      type: 1,
    }
  ],
  currentPay: 1,
  pay_type: 1,
  balance: 0,
  isLoading: false
  },
  ready: function () {
    if(this.data.taocan_type == 0){
      this.getList();
      
    }else {
      this.getGoldList();
      
    }
   
  },
  methods: {
    changePayType(e) {
      var item = e.currentTarget.dataset.item;
      var index = e.currentTarget.dataset.index;
      console.log(e.currentTarget, 'changePayTypechangePayType')
      this.setData({
        currentPay: index,
        pay_type: item.type
      })
      
    },
    getBalanceYun(){
      console.log('111111111111')
       var _this = this;
      api.post(getBlance, {
        uid: wx.getStorageSync('yz_uid')
      }).then(res => {
        //成功时回调函数
        console.log(res, 'getBlance')
        if (res.errno == 0) {
          _this.setData({
            balance: res.data
          })
        }
      }).catch(err => {
        
      })
    },
    buyNow() {
      if(this.data.isLoading){
        wx.showToast({
          title: '请勿重复点击',
          icon: 'none'
        })
        return 
      }
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        isLoading: true
      })
      console.log(this.data.openid, 'this.data.openid')
      var _this = this;
      api.post(createOrder, {
        pt_id: e.globalData.ptId,
        uid: wx.getStorageSync('yz_uid'),
        type: this.data.taocan_type == 0 ? 1 : 2, // 1充值积分2充值金币3购买套餐
        taocan_id: this.data.taocan_id,
        pay_type: this.data.pay_type, //1微信支付3余额支付 （只有买套餐有金币支付）
        openid: this.data.openid
      }).then(res => {
        //成功时回调函数
        console.log(res.errno, 'getCodeList')
        if (res.errno == 0) {
          var payInfo = res.data.payInfo;
          if(this.data.pay_type == 3 && this.data.taocan_type == 1){
            this.setData({
              isLoading: false
            })
            wx.hideLoading();
            wx.showToast({
              title: '充值成功',
              icon: 'none'
            })
            _this.getBalanceYun();
            _this.triggerEvent("confirm");
            _this.setData({
              show: false
            });
          }else {
            this.wechatPay(payInfo)
          }
         
        }else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.setData({
            isLoading: false
          })
          wx.hideLoading();
        }
      }).catch(err => {
        //失败时回调函数
        console.log(err)
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
      })
    },
    wechatPay(info) {
      var t = this;
      wx.requestPayment({
        timeStamp: info.timestamp,
        nonceStr: info.nonceStr,
        package: info.package,
        signType: info.signType ? info.signType : "MD5",
        paySign: info.paySign,
        success: function (a) {
          t.setData({
            isLoading: false
          })
          wx.hideLoading();
          wx.showModal({
            title: "提示",
            content: "支付成功",
            showCancel: !1,
            success: function (a) {
              if (a.confirm) {
                t.triggerEvent("confirm");
                t.setData({
                  show: false
                });
                // wx.navigateTo({
                //   url: "/haoduogeni/mine/index"
                // })
              }
            }
          });
        },
        fail: function (a) {
          console.log(a), wx.showToast({
            title: "支付失败请稍后重试",
            icon: "none",
            duration: 2e3
          });
          t.setData({
            isLoading: false
          })
          wx.hideLoading();
        }
      });
    },
    getGoldList(){
      var _this = this;
      api.post(getGoldList).then(res => {
        //成功时回调函数
        console.log(res.errno, 'getCodeList')
        if (res.errno == 0) {
          _this.setData({
            list: res.data,
            price: res.data[0].price,
            taocan_id: res.data[0].id
          })
        }
      }).catch(err => {
        //失败时回调函数
        console.log(err)
      })
      
    },
    changeTaocanType(e){
      console.log(e.currentTarget.dataset.type,'1111111')
      var type = e.currentTarget.dataset.type;
      if(type == 1) {
        this.setData({
          taocan_type: 0,
          current: 0,
          pay_type: 1
        })
        this.getList()
        
      }else if(type == 2) {
        this.setData({
          taocan_type: 1,
          current: 0,
          pay_type: 1
        })
        this.getGoldList();
        this.getBalanceYun();
      }
    },
    getList() {
      var _this = this;
      api.post(getPointList).then(res => {
        //成功时回调函数
        console.log(res.errno, 'getCodeList')
        if (res.errno == 0) {
          _this.setData({
            list: res.data,
            price: res.data[0].price,
            taocan_id: res.data[0].id
          })
        }
      }).catch(err => {
        //失败时回调函数
        console.log(err)
      })

    },
    changeTaocan(e) {
      console.log(e.currentTarget.dataset.index, 'eeeeeeeeeeeee')
      var index = e.currentTarget.dataset.index;
      var item = e.currentTarget.dataset.item;
      this.setData({
        current: index,
        taocan_id: item.id,
        price: item.price
      })
    },
    popupSpecsbtn: function (t) {
      console.log(t);
      this.triggerEvent("closeShow");
      this.setData({
        show: false
      });
    },
  }
});